package graph.day16;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Bellman_ford {

    static class Edge {
        final int from;
        final int to;
        final int weight;

        public Edge(int from, int to, int weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] minDist = new int[n + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);
        minDist[1] = 0;
        List<Edge> edges = new ArrayList<>();
        while (m-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int w = sc.nextInt();
            edges.add(new Edge(a, b, w));
        }
        for (int i = 1; i < n; i++) {
            for (Edge e : edges) {
                int from = e.from;
                int to = e.to;
                int w = e.weight;
                if (minDist[from] != Integer.MAX_VALUE && minDist[to] > minDist[from] + w) {
                    minDist[to] = minDist[from] + w;
                }
            }
        }
        if (minDist[n] == Integer.MAX_VALUE) System.out.println("unconnected");
        else System.out.println(minDist[n]);
    }
}
